package LeetCode.leetcode322;

import java.util.Arrays;

class Solution {
    public static void main(String[] args) {
        new Solution().coinChange(new int[]{1,2,5}, 11);
    }
    /**
     * coins = [1, 2, 5], amount = 11
     * @param coins
     * @param amount
     * @return
     */
    public int coinChange(int[] coins, int amount) {
        Arrays.sort(coins);
        int[] dp = new int[amount + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {  // 遍历背包
            for (int j = 0; j < coins.length; j++) { // 遍历物品
                if (i - coins[j] >= 0 && dp[i - coins[j]] != Integer.MAX_VALUE ) {
                    dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]);
                }
            }
        }
        if (dp[amount] == Integer.MAX_VALUE) return -1;
        return dp[amount];
    }
}